package com.gel.shortLink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gel.shortLink.convention.result.Result;
import com.gel.shortLink.convention.result.Results;
import com.gel.shortLink.dto.req.ShortLinkCreateReqDTO;
import com.gel.shortLink.dto.req.ShortLinkPageReqDTO;
import com.gel.shortLink.dto.req.ShortLinkUpdateReqDTO;
import com.gel.shortLink.dto.resp.ShortLinkCreateRespDTO;
import com.gel.shortLink.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.gel.shortLink.dto.resp.ShortLinkPageRespDTO;
import com.gel.shortLink.service.LinkDOService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class LinkController {

    private final LinkDOService linkDOService;

    /*短链接跳转*/
    @GetMapping("/{shortUri}")
    public  void restoreUrl(@PathVariable("shortUri") String shortUri, HttpServletRequest  request, HttpServletResponse  response){
        linkDOService.restoreUrl(shortUri,request,response);
    }


    /*创建短链接*/
    @PostMapping("/api/short-link/project/link/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
        ShortLinkCreateRespDTO result = linkDOService.createShortLink(requestParam);
        return Results.success(result);
    }
    /*分页查询短链接*/
    @GetMapping("/api/short-link/project/link/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink( @RequestBody ShortLinkPageReqDTO requestParam){
        IPage<ShortLinkPageRespDTO> result = linkDOService.pageShortLink(requestParam);
        return Results.success(result);
    }
    /*查询分组短链接数量*/
    @GetMapping("/api/short-link/project/link/list")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount( @RequestParam("requestParam") List<String> requestParam) {
        List<ShortLinkGroupCountQueryRespDTO> result=linkDOService.listGroupShortLinkCount(requestParam);
        return Results.success(result);

    }
    /*修改短连接信息*/
    @PutMapping("/api/short-link/project/link/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        linkDOService.updateShortLink(requestParam);
        return Results.success();
    }


}
